package Task24;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


import org.apache.log4j.Logger;


public class Controller {
        private static final Logger Log = Logger.getLogger(Controller.class);
        private boolean happendError = true;
        private Model myModel = new Model();
        private String workDir = "c:\\";
        
        public void setHappendError(boolean value) {
                happendError = value;
        }
        public boolean getHappendError() {
                return happendError;
        }


        public void setMyModel(Model value) {
                myModel = value;
        }
        public Model getMyModel() {
                return myModel;
        }


        public void setWorkDir(String value) {
                workDir = value;
        }
        public String getWorkDir() {
                return workDir;
        }


        
        // bug in move with case: create a new dir. Don't delete old file.
        public ArrayList<String> prompt() throws FileAlreadyExistException,
                        FileNotFoundException {
                ArrayList<String> sResult = new ArrayList<String>();
                File f = new File(workDir);
                if (!f.exists()) {
                        sResult.add("Prompt:" + workDir + "Not found directory.");
                        Log.error(sResult);
                } else {
                        if (f.isDirectory()) {
                                System.out.print(workDir + ">");
                                Scanner inp = new Scanner(System.in);
                                String command;
                                command = inp.nextLine();
                                Log.info("[workDir is: " + workDir + "] Input command: "
                                                + command);


                                sResult = tryRun(command);
                        } else {
                                sResult.add("Error. It is not directory " + f.toString());
                                Log.error(sResult);
                        }
                }
                return sResult;
        }


        public ArrayList<String> tryRun(String command)
                        throws FileAlreadyExistException, FileNotFoundException {
                ArrayList<String> sResult = new ArrayList<String>();
                happendError = true;
                myModel.setWorkDir(workDir);


                if (!command.startsWith("dir") && !command.startsWith("cd .."))
                        Log.info(command + " - Input command");


                try {
                        if (command.startsWith("cd"))
                                sResult = execute(parserForCd(command));
                        else
                                sResult = execute(parser(command));
                } catch (NullPointerException e) {
                        happendError = true;
                        sResult.add("No command");
                } catch (IOException e) {
                        sResult.add("Error. IOException.");
                } catch (DirectoryIsRootException e) {
                        sResult.add("Dir is root! Impossible command.");
                } catch (MyFileNotFoundException e) {
                        sResult.add("Not found directory");
                }


                if (happendError)
                        Log.error(sResult + " [workDir is: " + workDir + "]");
                else if (!command.startsWith("dir") && !command.startsWith("cd .."))
                        Log.info(command + " - Operation complete.");
                return sResult;
        }


        private CommandInterface parser(String command) throws NullPointerException {


                String[] arrayCommand = command.split(" ");


                // Define myModel
                myModel.setNumArg(arrayCommand.length - 1);
                if (myModel.getNumArg() > 0)
                        myModel.setArg1(arrayCommand[1]);
                if (myModel.getNumArg() > 1)
                        myModel.setArg2(arrayCommand[2]);
                myModel.setWorkDir(workDir);


                // Get instance of command
                String sCommand = arrayCommand[0];
                CommandInterface myCommand = myModel.getObjectCommand(sCommand);


                return myCommand;
        }


        private CommandInterface parserForCd(String command)
                        throws NullPointerException {


                String[] arrayCommand = command.split(" ");


                // Define myModel


                String sPath = "";


                if (arrayCommand.length > 1)
                        for (int i = 1; i < arrayCommand.length; i++) {
                                sPath += " " + arrayCommand[i];
                        }
                myModel.setArg1(sPath.substring(1, sPath.length()));
                myModel.setNumArg(1);
                myModel.setWorkDir(workDir);


                // Get instance of command
                String sCommand = arrayCommand[0];
                CommandInterface myCommand = myModel.getObjectCommand(sCommand);


                return myCommand;
        }


        private ArrayList<String> execute(CommandInterface myCommand)
                        throws FileNotFoundException, FileAlreadyExistException,
                        IOException, DirectoryIsRootException, MyFileNotFoundException {
                ArrayList<String> sResultMethod = new ArrayList<String>();
                String sError = myCommand.getError(); // Check for errors and change
                                                                                                // happendError flag!
                if (myCommand.happendError) {
                        sResultMethod.add(sError);
                        happendError = true;
                } else {
                        sResultMethod = myCommand.getResult();
                        happendError = false;
                }
                workDir = myModel.getWorkDir();
                return sResultMethod;
        }


        public static class FileAlreadyExistException extends Exception {
        }


        public static class DirectoryIsRootException extends Exception {
        }


        public static class MyFileNotFoundException extends Exception {
        }


}